﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace BasicComponents.utils
{
    public static class FormatUtils
    {
        //转换日期中的年份
        public static String getFormatedYearUpper(DateTime date)
        {
            char[] dateCharArray = date.Year.ToString().ToCharArray();
            String[] dateArray = new String[4];
            for (int i = 0; i < dateCharArray.Length; i++)
            {
                switch (dateCharArray[i])
                {
                    case '0':
                        dateArray[i] = "零";
                        break;
                    case '1':
                        dateArray[i] = "壹";
                        break;
                    case '2':
                        dateArray[i] = "贰";
                        break;
                    case '3':
                        dateArray[i] = "叁";
                        break;
                    case '4':
                        dateArray[i] = "肆";
                        break;
                    case '5':
                        dateArray[i] = "伍";
                        break;
                    case '6':
                        dateArray[i] = "陆";
                        break;
                    case '7':
                        dateArray[i] = "柒";
                        break;
                    case '8':
                        dateArray[i] = "捌";
                        break;
                    case '9':
                        dateArray[i] = "玖";
                        break;
                }
            }
            return String.Join("", dateArray);
        }
        //转换日期中的月份
        public static String getFormatedMonthUpper(DateTime date)
        {
            char[] dateCharArray = date.ToString("MM").ToCharArray();
            String month = "";
            //首先获取第一个字符，如果是0则直接为零，否则加拾
            switch (dateCharArray[0])
            {
                case '0':
                    month = "零";
                    break;
                case '1':
                    month = "壹拾";
                    break;
                case '2':
                    month = "贰拾";
                    break;
                case '3':
                    month = "叁拾";
                    break;
                case '4':
                    month = "肆拾";
                    break;
                case '5':
                    month = "伍拾";
                    break;
                case '6':
                    month = "陆拾";
                    break;
                case '7':
                    month = "柒拾";
                    break;
                case '8':
                    month = "捌拾";
                    break;
                case '9':
                    month = "玖拾";
                    break;
            }   //首先获取第一个字符，如果是0则直接为零，否则加拾
            switch (dateCharArray[1])
            {
                case '0':
                    month = "零壹拾";
                    break;
                case '1':
                    month += "壹";
                    break;
                case '2':
                    month += "贰";
                    break;
                case '3':
                    month += "叁";
                    break;
                case '4':
                    month += "肆";
                    break;
                case '5':
                    month += "伍";
                    break;
                case '6':
                    month += "陆";
                    break;
                case '7':
                    month += "柒";
                    break;
                case '8':
                    month += "捌";
                    break;
                case '9':
                    month += "玖";
                    break;
            }
            return month;
        }
        //转换日期中的天
        public static String getFormatedDayUpper(DateTime date)
        {
            char[] dateCharArray = date.ToString("dd").ToCharArray();
            String month = "";
            //首先获取第一个字符，如果是0则直接为零，否则加拾
            switch (dateCharArray[0])
            {
                case '0':
                    month = "零";
                    break;
                case '1':
                    month = "零壹拾";
                    break;
                case '2':
                    month = "零贰拾";
                    break;
                case '3':
                    month = "零叁拾";
                    break;
                case '4':
                    month = "零肆拾";
                    break;
                case '5':
                    month = "零伍拾";
                    break;
                case '6':
                    month = "零陆拾";
                    break;
                case '7':
                    month = "零柒拾";
                    break;
                case '8':
                    month = "零捌拾";
                    break;
                case '9':
                    month = "零玖拾";
                    break;
            }   //首先获取第一个字符，如果是0则直接为零，否则加拾
            switch (dateCharArray[1])
            {
                case '0':
                    break;
                case '1':
                    month = (month.Length == 1) ? month + "壹" : month.Substring(1) + "壹";
                    break;
                case '2':
                    month = (month.Length == 1) ? month + "贰" : month.Substring(1) + "贰";
                    break;
                case '3':
                    month = (month.Length == 1) ? month + "叁" : month.Substring(1) + "叁";
                    break;
                case '4':
                    month = (month.Length == 1) ? month + "肆" : month.Substring(1) + "肆";
                    break;
                case '5':
                    month = (month.Length == 1) ? month + "伍" : month.Substring(1) + "伍";
                    break;
                case '6':
                    month = (month.Length == 1) ? month + "陆" : month.Substring(1) + "陆";
                    break;
                case '7':
                    month = (month.Length == 1) ? month + "柒" : month.Substring(1) + "柒";
                    break;
                case '8':
                    month = (month.Length == 1) ? month + "捌" : month.Substring(1) + "捌";
                    break;
                case '9':
                    month = (month.Length == 1) ? month + "玖" : month.Substring(1) + "玖";
                    break;
            }
            return month;
        }
        //转换大写金额
        public static string getUpperRMBFromNumeric(decimal num)
        {
            string str1 = "零壹贰叁肆伍陆柒捌玖";            //0-9所对应的汉字
            string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
            string str3 = "";    //从原num值中取出的值
            string str4 = "";    //数字的字符串形式
            string str5 = "";  //人民币大写金额形式
            int i;    //循环变量
            int j;    //num的值乘以100的字符串长度
            string ch1 = "";    //数字的汉语读法
            string ch2 = "";    //数字位的汉字读法
            int nzero = 0;  //用来计算连续的零值是几个
            int temp;            //从原num值中取出的值

            num = Math.Round(Math.Abs(num), 2);    //将num取绝对值并四舍五入取2位小数
            str4 = ((long)(num * 100)).ToString();        //将num乘100并转换成字符串形式
            j = str4.Length;      //找出最高位
            if (j > 15) { return "溢出"; }
            str2 = str2.Substring(15 - j);   //取出对应位数的str2的值。如：200.55,j为5所以str2=佰拾元角分

            //循环取出每一位需要转换的值
            for (i = 0; i < j; i++)
            {
                str3 = str4.Substring(i, 1);          //取出需转换的某一位的值
                temp = Convert.ToInt32(str3);      //转换为数字
                if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
                {
                    //当所取位数不为元、万、亿、万亿上的数字时
                    if (str3 == "0")
                    {
                        ch1 = "";
                        ch2 = "";
                        nzero = nzero + 1;
                    }
                    else
                    {
                        if (str3 != "0" && nzero != 0)
                        {
                            ch1 = "零" + str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            ch1 = str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                    }
                }
                else
                {
                    //该位是万亿，亿，万，元位等关键位
                    if (str3 != "0" && nzero != 0)
                    {
                        ch1 = "零" + str1.Substring(temp * 1, 1);
                        ch2 = str2.Substring(i, 1);
                        nzero = 0;
                    }
                    else
                    {
                        if (str3 != "0" && nzero == 0)
                        {
                            ch1 = str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            if (str3 == "0" && nzero >= 3)
                            {
                                ch1 = "";
                                ch2 = "";
                                nzero = nzero + 1;
                            }
                            else
                            {
                                if (j >= 11)
                                {
                                    ch1 = "";
                                    nzero = nzero + 1;
                                }
                                else
                                {
                                    ch1 = "";
                                    ch2 = str2.Substring(i, 1);
                                    nzero = nzero + 1;
                                }
                            }
                        }
                    }
                }
                if (i == (j - 11) || i == (j - 3))
                {
                    //如果该位是亿位或元位，则必须写上
                    ch2 = str2.Substring(i, 1);
                }
                str5 = str5 + ch1 + ch2;

                if (i == j - 1 && str3 == "0")
                {
                    //最后一位（分）为0时，加上“整”
                    str5 = str5 + '整';
                }
            }
            if (num == 0)
            {
                str5 = "零元整";
            }
            return str5;
        }
        //转换大写金额
        public static string getUpperRMBFromStr(string numstr)
        {
            try
            {
                decimal num = Convert.ToDecimal(numstr);
                return getUpperRMBFromNumeric(num);
            }
            catch
            {
                return "非数字形式！";
            }
        }
        //传递金额，然后返回一个分割后的grid
        public static Grid getJEGrid(Decimal je, String bz, double width, int cout)
        {
            char[] jeArray = je.ToString("f2").Replace(".", "").ToCharArray();
            Grid grid = new Grid();
            grid.Width = width;
            for (int i = 0; i < cout; i++)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition());
            }
            //添加金额符号列 
            for (int i = jeArray.Length - 1; i >= 0; i--)
            {
                Label label = new Label();
                label.Padding = new Thickness(0);
                label.Margin = new Thickness(0);
                label.Content = jeArray[i];
                grid.Children.Add(label);
                label.SetValue(Grid.RowProperty, 0);
                label.SetValue(Grid.ColumnProperty, cout - 1 - (jeArray.Length - 1 - i));
            }    //添加符号
            Label label_bz = new Label();
            label_bz.Padding = new Thickness(0);
            label_bz.Margin = new Thickness(0);
            label_bz.Content = "￥";
            grid.Children.Add(label_bz);
            label_bz.SetValue(Grid.RowProperty, 0);
            label_bz.SetValue(Grid.ColumnProperty, cout - 1 - (jeArray.Length));
            return grid;
        }
    }
}
